//1.引入vue
import Vue from 'vue'
//2.下载并引入 vue-router
import VueRouter from 'vue-router'
//3.在vue中使用VueRouter
Vue.use(VueRouter)
//4.书写并暴露路由表

//路由懒加载的方式引入组件
const Home = () => import('@pages/Home')
const Login = () => import('@pages/Login')
const Register = () => import('@pages/Register')
const Search = () => import('@pages/Search')
const Detail = () => import('@pages/Detail')
const AddCartSuccess = () => import('@pages/AddCartSuccess')
const ShopCart = () => import('@pages/ShopCart')


const lastPush = VueRouter.prototype.push

VueRouter.prototype.push = function myPush(location, onComplete = () => { }, onAbort = () => { }) {
  //原本的push方法是对象点方法使用的,this指向VueRouter的实例,但是lastPush直接使用的话this指向undefined(打包的时候转为严格模式),在使用的时候内部涉及到this指向的时候就会报错,
  //myPush函数在调用时,是通过this.$router.push调用的,内部的this还是指向组件实例,所以return的函数需要修改this指向
  //
  return lastPush.call(this, location, onComplete, onAbort)
}

export default new VueRouter({
  scrollBehavior () {
    return { x: 0, y: 0}
    // return 期望滚动到哪个的位置
  },
  mode: 'history',
  routes: [
    {
      path: '/home',
      component: Home,
      name: 'home'
    },
    {
      path: '/login',
      component: Login,
      name: 'login',
      meta: {
        footerIsShow: true
      }
    },
    {
      path: '/detail/:skuId',
      component: Detail,
      name: 'detail',

    },
    {
      path: '/shopCart',
      component: ShopCart,
      name: 'shopCart',

    },
    {
      path: '/addCartSuccess',
      component: AddCartSuccess,
      name: 'addCartSuccess',

    },
    {
      path: '/register',
      component: Register,
      name: 'register',
      meta: {
        footerIsShow: true
      }

    },
    { path: '/search/:keyword?', component: Search, name: 'search' },
    // 配置路由重定向
    { path: '/', redirect: '/home' }

  ]
})